package com.bajiao.app.controller.recharge;

import cn.hutool.core.util.ObjectUtil;
import com.bajiao.app.base.AppBaseController;
import com.bajiao.common.api.vo.RestResult;
import com.bajiao.common.exception.DataHandleException;
import com.bajiao.common.util.FastJsonHelper;
import com.bajiao.entity.pay.wx.WxPayResult;
import com.bajiao.entity.recharge.RechargeMenu;
import com.bajiao.entity.user.User;
import com.bajiao.service.recharge.RechargeService;
import com.bajiao.vo.recharge.RechargeMenuVO;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.math.BigDecimal;
import java.util.List;

@Slf4j
@Api(tags = "充值")
@RestController
@RequestMapping("/api/v1/recharge")
public class RestRechargeController extends AppBaseController {

    @Resource
    private RechargeService rechargeService;

    @GetMapping(value = "/menu")
    @ApiOperation(value = "获取充值菜单")
    public RestResult<RechargeMenuVO> getRechargeMenu() {
        List<RechargeMenu> menuList = rechargeService.getMenuList();
        return success(FastJsonHelper.beanlistToeanList(menuList, RechargeMenuVO.class));
    }

    @ApiOperation(value = "微信充值")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "menuId", value = "菜单id", dataType = "String", required = true),
            @ApiImplicitParam(name = "source", value = "applet-小程序 ios-ios android-安卓", required = true, dataType = "String")
    })
    @PostMapping(value = "/wxPay")
    public RestResult<WxPayResult> wxPay(HttpServletRequest request,
                                         @ModelAttribute("user") User user,
                                         @RequestParam String menuId,
                                         @RequestParam String source) {
        if (ObjectUtil.isNull(user))
            return tokenOverdue();
        WxPayResult wxPayResult;
        try {
            wxPayResult = rechargeService.wxPay(request, user, menuId, source);
        } catch (DataHandleException d) {
            return this.fail(d.getMessage());
        }
        return success(wxPayResult);

    }
}
